﻿using BJ.AuthEntity;
using BJ.AuthEntity.Models;
using Syspetro.Core.AppDeal;
using Syspetro.Core.CommandBus;
using Syspetro.Core.DynamicApi;
using Syspetro.Core.Encrypt;
using Syspetro.Core.Extensions;
using Syspetro.Deploy.AuthorizationReq;

namespace BJ.AuthApp.Admin
{
    /// <summary>
    /// 初始化数据库种子数据(生产环境不生效)
    /// </summary>
    [RoleLevel(10)]
    public class InitDb : BaseDynamicApi
    {
        private readonly IFreeSql _freeSql;
        private readonly ICommandBus _command;
        public InitDb(IFreeSql freeSql,ICommandBus command)
        {
            _freeSql = freeSql;
            _command = command;
        }
        public SpActionResult<string> InitAdminUser()
        {
            AdminRole role = new() { IsAdmin = true, Level = 10, Name = "超级管理员" };
            AdminUser user = new() { Name = "系统管理员", RoleId = role.Id, Phone = "17302360304", Enable2FA = "488249" };

            var pswd = _command.Send(new PswdMd5Command {Pswd = "123456" });

            AdminUserAuth auth = new() { Account = "admin", Password = pswd.Get().Data, Id = user.Id, AccountType = AccountType.Other };
            user.Auth = auth;
            _freeSql.Insert(role).ExecuteAffrows();
            _freeSql.Insert(user).ExecuteAffrows();
            _freeSql.Insert(auth).ExecuteAffrows();
            return Result("已默认添加管理员用户,登录名'admin'密码为:'123456',Google验证:otpauth://totp/系统管理员?secret=OF3XK2LEMZUGU3TZPBVWYYJUHA4DENBZ&issuer=GMP");
        }
        public SpActionResult<string> InitUser()
        {
            User user = new() { Name = "bjkj", Phone = "17302360304", Enable2FA = "488249" };
            var pswd = _command.Send(new PswdMd5Command {Pswd = "123456" });

            UserAuth auth = new() { Account = "bjkj", Password = pswd.Get().Data, Id = user.Id, AccountType = AccountType.Other };
            user.Auth = auth;
            _freeSql.Insert(user).ExecuteAffrows();
            _freeSql.Insert(auth).ExecuteAffrows();
            return Result("已默认添加用户,登录名'bjkj'密码为:'123456',Google验证:otpauth://totp/bjkj?secret=OF3XK2LEMZUGU3TZPBVWYYJUHA4DENBZ&issuer=GMP");
        }
    }
}
